package hotLeetcode;

import java.util.GregorianCalendar;
import java.util.HashSet;

public class H08Leetcode3 {
    public int lengthOfLongestSubstring(String s) {
        //思路1 暴力
        int length = 0;
        for (int i = 0; i < s.length(); i++) {
            HashSet<Character> set = new HashSet<>();
            for (int j = i; j < s.length(); j++) {
                char c = s.charAt(j);
                if (set.contains(c)) {
                    break;
                }
                set.add(c);
                length = Math.max(length, j - i + 1);
            }

        }

        return length;

    }


    //思路2 滑动窗口
    public int lengthOfLongestSubstring2(String s) {
        char[] charArray = s.toCharArray();
        HashSet<Character> set = new HashSet<>();

        int left = 0, result = 0;
        for (int right = 0; right < charArray.length; right++) {
            char c = charArray[right];
            while (set.contains(c)) {
                set.remove(charArray[left]);
                left++;
            }
            set.add(c);
            result = Math.max(result, right - left + 1);

        }


        return result;
    }





    public static void main(String[] args) {
        String s = "pwwkew";
        H08Leetcode3 h08Leetcode3 = new H08Leetcode3();
        System.out.println(h08Leetcode3.lengthOfLongestSubstring2(s));
    }



}
